package com.bicycle.rentalsystem.listener;



import com.bicycle.rentalsystem.jdbc.JdbcUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@WebListener
public class ApplicationContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Servlet上下文初始化");
        ServletContext context = sce.getServletContext();
        String jdbcConfig = context.getInitParameter("jdbcConfig");
        InputStream is = this.getClass().getResourceAsStream(jdbcConfig);
        Properties props = new Properties();
        try {
            props.load(is);
            JdbcUtil.initDataSource(props);
            System.out.println(props);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        JdbcUtil.destroyDataSource();
        System.out.println("Servlet上下文销毁");
    }
}
